# coding: utf-8
import re
import urllib
import tornado.web
from tornado.options import define, options
from app.helper.util import storify,sha1,md5



class _Context(dict):
    """Template context container.

    A container which will return empty string silently if the key is not exist
    rather than raise AttributeError when get a item's value.

    It will raise TemplateContextError if debug is True and the key
    does not exist.

    The context item also can be accessed through get attribute.

    """
    def __setattr__(self, key, value):
        self[key] = value

    def __str__(self):
        return str(self)

    def __iter__(self):
        return iter(self.items())

    def __getattr__(self, key):
        """Get a context attribute.

        Raise TemplateContextError if the attribute not be set to
        avoid confused AttributeError exception when debug is True, or return ""

        """
        if key in self:
            return self[key]
        elif options.debug:
            raise TemplateContextError("'%s' does not exist in context" % key)
        else:
            return ""

    def __hasattr__(self, key):
        if key in self:
            return True
        else:
            return False

class TemplateContextError():
    """Template context variable does not exist."""
    pass


